package com.qijian.maindata.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Anonymous;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.core.redis.RedisCache;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.DateUtils;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.maindata.domain.ExchangeRate;
import com.qijian.tool.domain.query.ExchangeRateQuery;
import com.qijian.maindata.service.IExchangeRateService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 汇率Controller
 *
 * @author qijian
 * @date 2022-03-04
 */
@Api(tags = "汇率")
@RestController
@RequiredArgsConstructor
@RequestMapping("/finance/ExchangeRate")
public class ExchangeRateController extends BaseController {

    private final IExchangeRateService exchangeRateService;
    private final RedisCache redisCache;

    /**
     * 查询汇率列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ExchangeRateQuery query, IPage<ExchangeRate> page) {
        return getDataTable(exchangeRateService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /***
     * 查询汇率列表 (匿名，用于外部供应商，比如外协竞价发送给供应商的邮件)
     */
    @Anonymous
    @GetMapping("/listA")
    public TableDataInfo listAnonymous(ExchangeRateQuery query, IPage<ExchangeRate> page) {
        return getDataTable(exchangeRateService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    @Anonymous
    @GetMapping("/listchange/{uuid}")
    public AjaxResult listByUuid(@PathVariable("uuid") String uuid, ExchangeRateQuery query) {
        if (CollectionUtil.isEmpty(redisCache.keys("*".concat(uuid)))) {
            throw new ServiceException("E01:无权限操作");
        }
        return AjaxResult.success(exchangeRateService.list(WhereEntityTool.invoke(query)));
    }

    /**
     * 获取汇率详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:ExchangeRate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(exchangeRateService.getById(id));
    }

    @GetMapping(value = "/exchange")
    public AjaxResult getExchangeRate(ExchangeRateQuery query) {
        if (StringUtils.isBlank(query.getCurrencyType())) {
            throw new ServiceException("E01:币别不能为空！");
        }
        if (StringUtils.isBlank(query.getMonth())) {
            query.setMonth(DateUtils.dateTimeNow(DatePattern.NORM_MONTH_PATTERN));
        }
        query.setIsEnabled(true);
        return AjaxResult.success(exchangeRateService.getOne(WhereEntityTool.invoke(query)));
    }

    /**
     * 新增汇率
     */
    @PreAuthorize("@ss.hasPermi('finance:ExchangeRate:add')")
    @Log(title = "汇率", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExchangeRate exchangeRate) {
        ExchangeRateQuery query = new ExchangeRateQuery();
        query.setCurrencyType(exchangeRate.getCurrencyType());
        query.setMonth(exchangeRate.getMonth());
        if (exchangeRateService.count(WhereEntityTool.invoke(query)) > 0) {
            throw new ServiceException(exchangeRate.getMonth() + "，" + exchangeRate.getCurrencyType() + " 已存在！");
        }
        return toAjax(exchangeRateService.save(exchangeRate));
    }

    /**
     * 修改汇率
     */
    @PreAuthorize("@ss.hasPermi('finance:ExchangeRate:edit')")
    @Log(title = "汇率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExchangeRate exchangeRate) {
        //已启用则不允许修改
        ExchangeRate rate = exchangeRateService.getById(exchangeRate.getId());
        if (rate.getIsEnabled()) {
            throw new ServiceException("E01:已启用则不允许修改!");
        }
        return toAjax(exchangeRateService.updateById(exchangeRate));
    }
}
